<?php
/**
 * 优惠券
 */
namespace app\ais\controller\api\v1;
use app\ais\controller\api\Base;
use app\ais\model\AisCoupon;
use app\ais\model\AisStore;
use app\ais\model\AisCouponUser;
use think\Db;

class Gmcoupon extends Base{

    protected $store;

    public function initialize() {
        parent::initialize();
        $this->isUserAuth();
        $this->store = AisStore::manageStore($this->user->id);
        if(empty($this->store)){
            exit(json_encode(['code' => 403,'msg'=>'无法找到该商家']));
        }
    }

   /**
     * 指定商家的优惠券
     * @return void
     */
    public function index(){
        $this->apiSign();
        $coupon = AisCoupon::where(['member_miniapp_id' => $this->miniapp_id,'store_id' => $this->store->id,'is_end' => 0])->where('types','<',3)->order('is_top desc,sort desc,create_time desc')->select();
        if ($coupon->isEmpty()) {
            return enjson(204);
        }
        return enjson(200,$coupon);
    }

    /**
     * 显示单个优惠券信息
     * @return void
     */
    public function read(){
        $param['id']        = $this->request->param('id');  //优惠券id
        $this->apiSign($param);
        $info = AisCoupon::where(['member_miniapp_id' => $this->miniapp_id,'id' => $param['id'],'store_id' => $this->store->id])->find();
        if (empty($info)) {
            return enjson(404,'未找到优惠券');
        }
        $count = AisCouponUser::where(['coupon_id' => $info->id])->count(); //已领取
        $order = AisCouponUser::where(['coupon_id' => $info->id,'is_end' => 0,'money' => 0])->count();  //待使用
        $money = AisCouponUser::where(['coupon_id' => $info->id,'is_end' => 1])->sum('money');  //共优惠
        return enjson(200,['coupon' => $info,'count' => $count,'order' => $order,'money' => $money]);
    }

    /**
     * 添加和修改
     */
    public function edit(){
        $param['id']        = $this->request->param('id');
        $param['types']     = $this->request->param('types/d',0);
        $param['price']     = $this->request->param('price/d',0);
        $param['amount']    = $this->request->param('amount/d',0);
        $param['discount']  = $this->request->param('discount/s',0);
        $param['num']       = $this->request->param('num/d',1);
        $param['howmuch']   = $this->request->param('howmuch/d',0);
        $param['name']      = $this->request->param('name/s');
        $param['tips']      = $this->request->param('tips/s');
        $this->apiSign($param);
        $validate = $this->validate($param,'Coupon.edit');
        if(true !== $validate){
            return enjson(403,$validate);
        }
        switch ($param['types']) {
            case 1: //折扣券
                $param['amount']   = 0;
                $param['price']    = 0;
                break;
            case 2: //兑换券
                $param['discount'] = 0;
                $param['price']    = 0;
                break;
            default: //代金券
                $param['discount'] = 0;
                $param['amount']   = 0;
                break;
        }
        $param['is_top']  = 0;
        $param['citycode'] = $this->store->citycode;
        $coupon = new AisCoupon();
        if($param['id']){
            $info = $coupon->where(['member_miniapp_id' => $this->miniapp_id,'id' => $param['id']])->find();
            if(!$info){
                return enjson(403,'未找到要修改的优惠券');
            }
            if($info->is_lock){
                return enjson(403,'本券禁止修改');
            }
            $result = $coupon->allowField(true)->save($param,['id' => $param['id']]);
        }else{
            $param['member_miniapp_id'] = $this->miniapp_id;
            $param['store_id']          = $this->store->id;
            $param['create_time']       = time();
            $param['update_time']       = time();
            $result                     = $coupon->create($param);
        }
        return enjson(200,'优惠券设置成功');
    }


     //商户删除优惠券
    public function delete(){
        $param['id'] = $this->request->param('id/d',0);
        $this->apiSign($param);
        $coupon = AisCoupon::where(['member_miniapp_id' => $this->miniapp_id,'store_id' => $this->store->id,'id' => $param['id'],'is_lock' => 0,'is_end' => 0])->find();
        if (empty($coupon)) {
            return enjson(404,'当前优惠券禁止删除');
        }
        $coupon->is_end = 1;
        $coupon->save();
        return enjson(200,'成功删除优惠券');
    }
}